<?php

namespace app\achievement\controller;
use app\common\service\CompanyService;
use think\Controller;
use app\common\model\Node;
use app\common\model\Notice;

use think\Config;
use think\Cookie;
use app\common\model\User;
use think\Db;


header("Content-type: text/html; charset=utf-8");

class Base extends Controller {

    protected $set = array();

    public function __construct() {
        parent::__construct();
        header("Cache-control:private"); //开启网页表单缓存，一定要放在Session_start 函数后
    }

    public function _initialize() {
        //初始化权限数据表
        $this->set['db_name'] = 'achievement_user';
        $this->set['auth_group'] = 'achievement_auth_group';
        $this->set['auth_group_access'] = 'achievement_auth_group_access';
        $this->set['auth_rule'] = 'achievement_auth_rule';
        $this->set['model'] = strtolower(request()->module());
        $this->company_id = session($this->set['db_name'] . 'company_id');
        // 根据表名设置session避免多个端session被覆盖
        if (!session($this->set['db_name'] . 'uid') || !session($this->set['db_name'] . 'username')) {
            //判断是否存在免登cookie
            if (empty(Cookie::get(strtolower(request()->module()) . '_user'))) {
                $this->redirect('login/index');
            } else {
                $hasUser = Cookie::get(strtolower(request()->module()) . '_user');

                $userOne = Db::name($this->set['db_name'])->field('status,is_delete,groupid')->where(['id' => $hasUser['id']])->find();

                //判断帐号是否异常
                if ($userOne['status'] == 0 || $userOne['is_delete'] == 1) {
                    $this->redirect('login/index');
                }


                //获取模型
                $module = strtolower(request()->module());
                $user = new User($this->set['db_name']);
                //获取用户分组
                $info = $user->getRoleInfo($hasUser['groupid'], $module);
                session($this->set['db_name'] . 'company_id', $hasUser['company_id']);
                session($this->set['db_name'] . 'uid', $hasUser['id']);         //用户ID
                session($this->set['db_name'] . 'username', $hasUser['account']);  //用户名
                session($this->set['db_name'] . 'portrait', $hasUser['avatar_img']); //用户头像
                session($this->set['db_name'] . 'rolename', $info['title']);    //角色名
                session($this->set['db_name'] . 'is_admin', $hasUser['is_admin']);
                session($this->set['db_name'] . 'groupid', $hasUser['groupid']);
                //如果为超管登录 清除角色节点
                if (empty($info['rules'])) {
                    session($this->set['db_name'] . 'rule', null);        //角色节点
                } else {
                    session($this->set['db_name'] . 'rule', $info['rules']);        //角色节点
                }

                if (!empty($info['name'])) {
                    session($this->set['db_name'] . 'name', $info['name']);         //角色权限
                }
                $param = [
                    'loginnum' => $hasUser['loginnum'] + 1,
                    'last_login_ip' => request()->ip(),
                    'last_login_time' => time()
                ];
                Db::name($this->set['db_name'])->where('id', $hasUser['id'])->update($param);
                writelog($hasUser['id'], session($this->set['db_name'] . 'username'), '用户【' . session($this->set['db_name'] . 'username') . '】登录成功', 1, $this->set['model'] . '_log');
            }
        }



        $auth = new \com\Auth($this->set);
        $module = strtolower(request()->module());
        $controller = strtolower(request()->controller());
        $action = strtolower(request()->action());
        $url = $module . "/" . $controller . "/" . $action;
        //跳过检测以及主页权限
        if (session($this->set['db_name'] . 'is_admin') != 1 && $controller != 'index') {
            $excludeCheck = Config::get('filter_config.' . strtolower(request()->module()));
            if (!in_array($url, $excludeCheck)) {
                if (!$auth->check($url, session($this->set['db_name'] . 'uid'))) {
                    $this->error('抱歉，您没有操作权限');
                }
            }
        }

        $node = new Node();

        if (empty(session($this->set['db_name'] . 'rule')) && session($this->set['db_name'] . 'is_admin') != 1) {
            $auth_rule = '';
        } else {
            $auth_rule = $node->getMenu(session($this->set['db_name'] . 'rule'), $this->set['auth_rule']);
        }

        // 获取入驻商图片
        $result = $result = CompanyService::getCompanyConfig($this->company_id);


        //统计公告条数
        $map['status'] = 1;
        $map['isshow'] = 1;
        $ann_model = new Notice();
        $ann_count = $ann_model->where($map)->count();
        $this->assign(['username' => session($this->set['db_name'] . 'username'),
            'uid' => session($this->set['db_name'] . 'uid'),
            'portrait' => session($this->set['db_name'] . 'portrait'),
            'rolename' => session($this->set['db_name'] . 'rolename'),
            'menu' => $auth_rule, //读取权限菜单
            'is_admin' => session($this->set['db_name'] . 'is_admin'), //是否为超管
            'menuurl' => $url,
            'controller' => $controller,
            'action' => $action,
            'ann_count' => $ann_count,
            'socket_url' => config('socket_url'),
            'company_config' => $result
        ]);
    }

}
